<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 输入框 -->
          <el-input
            placeholder="请输入内容"
            v-model="query.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <!-- 添加按钮 -->
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-table :data="getlist" border stripe>
          <el-table-column type="index"> </el-table-column>
          <el-table-column prop="goods_name" label="商品名称">
          </el-table-column>
          <el-table-column
            prop="goods_price"
            label="商品价格（元）"
            width="110px"
          >
          </el-table-column>
          <el-table-column prop="goods_weight" label="商品重量" width="100px">
          </el-table-column>
          <el-table-column prop="add_time" label="创建时间" width="150px">
            <template slot-scope="scope">
              {{ scope.row.add_time | fromData }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="140px">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
              ></el-button>
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="removegoods(scope.row.goods_id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.pagenum"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="query.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 列表请求参数
      query: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表数据
      getlist: [],
      //   总条数
      total: 0
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    //   数据列表
    async getGoodsList() {
      const { data: res } = await this.$axios.get('goods', {
        params: this.query
      })
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.$message.success('获取成功')
      console.log(res)
      this.total = res.data.total
      this.getlist = res.data.goods
      console.log(this.getlist)
    },
    // 页面条改变
    handleSizeChange(newsize) {
      this.query.pagesize = newsize
      // 属性数据
      this.getGoodsList()
    },
    // 页面改变
    handleCurrentChange(newnum) {
      this.query.pagenum = newnum
      this.getGoodsList()
    },
    // 删除数据
    async removegoods(id) {
      const removeconfirm = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)

      if (removeconfirm === 'cancel')
        return this.$message.info('取消删除成功！')
      const { data: res } = await this.$axios.delete('goods/' + id)
      if (res.meta.status !== 200) return this.$message.error('删除失败')
      this.$message.success('删除成功')
      this.getGoodsList()
    },
    // 添加商品
    goAddpage() {
      this.$router.push({
        path: '/goods/add'
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
